package com.situ.mall.admin.controller;


import com.situ.mall.base.entity.Banner;
import com.situ.mall.base.service.IBannerService;
import com.situ.mall.base.util.C;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.LayuiTableVO;
import com.situ.mall.base.vo.ResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author darling
 * @since 2023-03-22
 */
@Controller
@RequestMapping(URLUtil.BANNER)
public class BannerController {

    @Autowired
    private IBannerService bannerService;

    // banner管理页
    @GetMapping(URLUtil.PAGE)
    public String page() {
        return URLUtil.BANNER_HTML;
    }

    // 查询banner列表
    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO doList(@RequestParam Integer page, @RequestParam Integer limit, String keyword) {
        return bannerService.doList(page, limit, keyword);
    }

    // 修改banner
    @PostMapping(URLUtil.EDIT + URLUtil.UID)
    @ResponseBody
    public Banner edit(@PathVariable("id") Integer id) {
        return bannerService.getById(id);
    }

    // 添加或修改banner
    @PostMapping(URLUtil.SAVE)
    @ResponseBody
    public void save(Banner banner) {
        if (banner.getId() == null) {
            banner.setCreatetime(LocalDateTime.now());
            banner.setStatus(0);
        }
        bannerService.saveOrUpdate(banner);
    }

    // 上传banner图
    @PostMapping(URLUtil.IMAGES)
    @ResponseBody
    public ResVO avatar(MultipartFile file) {
        return bannerService.avatar(file);
    }

    // 删除banner
    @PostMapping(URLUtil.DELETE + URLUtil.UID)
    @ResponseBody
    public void deleteById(@PathVariable("id") Integer id){
        bannerService.deleteById(id);
    }

    // 恢复删除的banner
    @PostMapping(URLUtil.REGAIN + URLUtil.UID)
    @ResponseBody
    public void regainById(@PathVariable("id") Integer id){
        bannerService.regainById(id);
    }

    // 查看已删除的banner
    @GetMapping(URLUtil.DELETE + URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO doDelList(@RequestParam Integer page, @RequestParam Integer limit, String keyword) {
        return bannerService.doDelList(page, limit, keyword);
    }

    // Banner展示
    @PostMapping(URLUtil.START)
    @ResponseBody
    public void start(@RequestParam("ids[]") Integer[] ids) {
        bannerService.doStartOrStop(ids, 1);
    }

    // Banner取消展示
    @PostMapping(URLUtil.STOP)
    @ResponseBody
    public void stop(@RequestParam("ids[]") Integer[] ids) {
        bannerService.doStartOrStop(ids, 0);
    }


}
